constraint guide: Make strength tweakable
authorMatthias Clasen <mclasen@redhat.com>
Sat, 29 Jun 2019 14:18:23 +0000 (14:18 +0000)
committerEmmanuele Bassi <ebassi@gnome.org>
Sun, 30 Jun 2019 23:10:11 +0000 (00:10 +0100)
The strength for the natural width can be used
as a tie-breaker to make instable systems behave
in a more predictable way. This can be seen
in the simple constraints demo in gtk-demo.

demos/gtk-demo/constraints.c
gtk/gtkconstraintguide.c
gtk/gtkconstraintguide.h

index 9e2bf3577d7492c4073f1559c3db77a9035e4878..1b6b00b2f5bd52f43c15574097641d928714ce92 100644 (file)
@@ -88,6 +88,7 @@ build_constraints (SimpleGrid          *self,
   gtk_constraint_guide_set_min_size (guide, 10, 10);
   gtk_constraint_guide_set_nat_size (guide, 100, 10);
   gtk_constraint_guide_set_max_size (guide, 200, 20);
+  gtk_constraint_guide_set_strength (guide, GTK_CONSTRAINT_STRENGTH_STRONG);
   gtk_constraint_layout_add_guide (manager, guide);
 
   gtk_constraint_layout_add_constraint (manager,
index 60220c60f6027b912049c292f7d3cff947dea5cf..b6db1554b228f1da8588458991d7329f28c5974b 100644 (file)
@@ -47,6 +47,8 @@ struct _GtkConstraintGuide
 
   char *name;
 
+  int strength;
+
   int values[LAST_VALUE];
 
   GtkConstraintLayout *layout;
@@ -73,6 +75,7 @@ enum {
   PROP_NAT_HEIGHT,
   PROP_MAX_WIDTH,
   PROP_MAX_HEIGHT,
+  PROP_STRENGTH,
   PROP_NAME,
   LAST_PROP
 };
@@ -137,7 +140,7 @@ gtk_constraint_guide_update_constraint (GtkConstraintGuide *guide,
       guide->constraints[index] =
         gtk_constraint_solver_add_stay_variable (solver,
                                                  var,
-                                                 GTK_CONSTRAINT_WEIGHT_MEDIUM);
+                                                 guide->strength);
     }
   else
     {
@@ -232,6 +235,10 @@ gtk_constraint_guide_set_property (GObject      *gobject,
         }
       break;
 
+    case PROP_STRENGTH:
+      gtk_constraint_guide_set_strength (self, g_value_get_enum (value));
+      break;
+
     case PROP_NAME:
       gtk_constraint_guide_set_name (self, g_value_get_string (value));
       break;
@@ -261,6 +268,10 @@ gtk_constraint_guide_get_property (GObject    *gobject,
       g_value_set_int (value, self->values[prop_id - 1]);
       break;
 
+    case PROP_STRENGTH:
+      g_value_set_int (value, self->strength);
+      break;
+
     case PROP_NAME:
       g_value_set_string (value, self->name);
       break;
@@ -335,6 +346,15 @@ gtk_constraint_guide_class_init (GtkConstraintGuideClass *class)
                         G_PARAM_READWRITE|
                         G_PARAM_EXPLICIT_NOTIFY);
 
+  guide_props[PROP_STRENGTH] =
+      g_param_spec_enum ("strength",
+                         "Strength",
+                         "The strength to use for natural size",
+                         GTK_TYPE_CONSTRAINT_STRENGTH,
+                         GTK_CONSTRAINT_STRENGTH_MEDIUM,
+                         G_PARAM_READWRITE|
+                         G_PARAM_EXPLICIT_NOTIFY);
+
   guide_props[PROP_NAME] =
       g_param_spec_string ("name",
                            "Name",
@@ -538,3 +558,27 @@ gtk_constraint_guide_set_name (GtkConstraintGuide *guide,
   guide->name = g_strdup (name);
   g_object_notify_by_pspec (G_OBJECT (guide), guide_props[PROP_NAME]);
 }
+
+GtkConstraintStrength
+gtk_constraint_guide_get_strength (GtkConstraintGuide *guide)
+{
+  g_return_val_if_fail (GTK_IS_CONSTRAINT_GUIDE (guide),
+                        GTK_CONSTRAINT_STRENGTH_MEDIUM);
+
+  return guide->strength;
+}
+
+void
+gtk_constraint_guide_set_strength (GtkConstraintGuide    *guide,
+                                   GtkConstraintStrength  strength)
+{
+  g_return_if_fail (GTK_IS_CONSTRAINT_GUIDE (guide));
+
+  if (guide->strength == strength)
+    return;
+
+  guide->strength = strength;
+  g_object_notify_by_pspec (G_OBJECT (guide), guide_props[PROP_STRENGTH]);
+  gtk_constraint_guide_update_constraint (guide, NAT_WIDTH);
+  gtk_constraint_guide_update_constraint (guide, NAT_HEIGHT);
+}
index 429085c1f9ea63af84076dcb1e873a0225a9bf32..1a489543f83488c57545f05922b2f29a20aaf6fe 100644 (file)
@@ -21,6 +21,7 @@
 
 #include <gtk/gtktypes.h>
 #include <gtk/gtkenums.h>
+#include <gtk/gtktypebuiltins.h>
 
 G_BEGIN_DECLS
 
@@ -67,6 +68,13 @@ GDK_AVAILABLE_IN_ALL
 void                    gtk_constraint_guide_get_max_size       (GtkConstraintGuide *guide,
                                                                  int                *width,
                                                                  int                *height);
+
+GDK_AVAILABLE_IN_ALL
+GtkConstraintStrength   gtk_constraint_guide_get_strength       (GtkConstraintGuide *guide);
+GDK_AVAILABLE_IN_ALL
+void                    gtk_constraint_guide_set_strength       (GtkConstraintGuide    *guide,
+                                                                 GtkConstraintStrength  strength);
+
 GDK_AVAILABLE_IN_ALL
 void                    gtk_constraint_guide_set_name           (GtkConstraintGuide *guide,
                                                                  const char         *name);